package com.yijia.work5.question4;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class Question4Servlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        if (request.getParameter("radius").equals("")){

            System.out.println("半径为空！");
            request.setAttribute("error", "半径值为空！！");
            request.getRequestDispatcher("/work5/4.jsp").forward(request, response);

        }else {
            Question4Bean question4Bean = new Question4Bean();

            try{
            int radius = Integer.parseInt(request.getParameter("radius"));
                double area = question4Bean.area(radius);
                double perimeter = question4Bean.perimeter(radius);
                System.out.println("半径："+radius);
                System.out.println("面积："+area);
                System.out.println("周长："+perimeter);


                request.setAttribute("radius", radius);
                request.setAttribute("area", area);
                request.setAttribute("perimeter", perimeter);

            }catch (NumberFormatException e){
                System.out.println("接收到的中非数值！");
                request.setAttribute("error", "半径值必须为数值类型！！");
            }finally {
                request.getRequestDispatcher("/work5/4.jsp").forward(request, response);

            }


        }




    }

}
